package xf.tools;

import android.content.Context;
import android.os.Handler;

import com.emm.yixun.mobile.application.EmmApplication;
import com.yiguo.toast.Toast;

import com.eroad.product.tools.ToastCompat;

/**
 * 自定义Toast，剔除多个Toast产生的叠加效果
 */
public class CustomToast {
    private static Toast mToast;
    private static Handler mHandler = new Handler();
    private static Runnable r = new Runnable() {
        public void run() {
            mToast.cancel();
        }
    };

    public static void disToast() {
        mToast.cancel();
    }

    public static void showToast(Context mContext, String text, int duration) {
        if (!EmmApplication.isNull(text)) {
            return;
        }
        if (text.indexOf(EmmApplication.TOAST_STR) > 0) {
            if ("1".equals(EmmApplication.getData(EmmApplication.PROJECT_LIST_ISNULL))) {
                text = "项目列表为空";
            } else {
                text = "该项目不存在";
            }
        }

        mHandler.removeCallbacks(r);
        if (mToast != null)
            mToast.setText(text);
        else
            mToast = Toast.makeText(mContext, text, ToastCompat.LENGTH_SHORT);
        mHandler.postDelayed(r, duration);
        mToast.show();
    }

    public static void showToast(Context mContext, int resId, int duration) {
        showToast(mContext, mContext.getResources().getString(resId), duration);
    }
}
